#include <bits/stdc++.h>
#define gc getchar
#define pc putchar
#define pt printf
#define int long long
#define For(i,a,b) for(int i=a;i<=b;++i)
#define Rof(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int Mxn=405;
int N,Tp,Ans,A[Mxn],B[Mxn],F[Mxn],Sk[Mxn];
int Ct,Nt[Mxn<<1],To[Mxn<<1],Hd[Mxn]; 
int _() {
	int x=0,f=0;char ch=gc();
	while(ch<'0'||ch>'9') f|=(ch=='-'),ch=gc();
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=gc();
	return f?-x:x;	
}
void __(int x) {
	int stk[30],tp=0;
	if(x<0) x=-x,pc('-');
	do stk[++tp]=x%10,x/=10;while(x);
	while(tp) pc(stk[tp--]^48);
}
void Merge(int x,int y) {
	Nt[++Ct]=Hd[x],To[Ct]=y,Hd[x]=Ct;
	Nt[++Ct]=Hd[y],To[Ct]=x,Hd[y]=Ct;
}
int Calc() {
	int Res=0;
	For(i,1,N) B[i]=A[i];
	For(i,1,Tp) {
		int x=Sk[i];
//		pt("%d ",x);
		Res+=B[x];
		for(int k=Hd[x];k;k=Nt[k]) {
			B[To[k]]+=B[x];
		} 
	}
//	pc('\n');
	return Res;
}
void DFS(int x) {
	if(x>N) {Ans=max(Ans,Calc());return ;}
	For(i,1,N) {
		if(!F[i]) F[i]=1,Sk[++Tp]=i,DFS(x+1),--Tp,F[i]=0;
	}
}
signed main() {
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	N=_();
	For(i,1,N) A[i]=B[i]=_();
	For(i,2,N) Merge(_(),_());
	DFS(1);
	__(Ans);
	return 0;
}
